#region Microsoft Public License
/*
 * (c) Copyright Chenjun Wu. http://utablesdk.codeplex.com
 * 
 * This source is subject to the Microsoft Public License (Ms-PL).
 * Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
 * All other rights reserved.
 */
#endregion

﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using UTable.Objects.Handlers;

namespace UTable.Objects.Controls
{
    public class FileItemViewBase: UUserControl
    {
        public FileItemViewBase()
        {
            // Handle tap gesture
            SingleFingerHandler handler = new SingleFingerHandler(this);
            handler.SingleFingerDown += new SingleFingerEventHandler(handler_SingleFingerDown);
            handler.SingleFingerMove += new SingleFingerEventHandler(handler_SingleFingerMove);
            handler.SingleFingerUp += new SingleFingerEventHandler(handler_SingleFingerUp);
            handler.CaptureFinger = false;
            handler.StopRouteDown = false;
            this.MultiTouchProcessor.Handlers.Add(handler);
        }

        private Point FingerDownPosition;
        private double MaxTapDistance = 10;
        private bool NoTap;

        void handler_SingleFingerUp(SingleFingerHandler sender, UTable.Input.MultiTouch.FingerEventArgs args)
        {
            if (!NoTap)
            {
                TrySelectFile();
                
            }
        }

        void handler_SingleFingerMove(SingleFingerHandler sender, UTable.Input.MultiTouch.FingerEventArgs args)
        {
            if (!NoTap && (args.GlobalPosition - FingerDownPosition).Length > MaxTapDistance)
                NoTap = true;
        }

        void handler_SingleFingerDown(SingleFingerHandler sender, UTable.Input.MultiTouch.FingerEventArgs args)
        {
            NoTap = false;
            FingerDownPosition = args.GlobalPosition;
        }

        void TrySelectFile()
        {
            if (FileExplorer.SelectFile(this.DataContext as FileItemData))
            {
            }
        }

        private UFileExplorer fileExplorer = null;

        public UFileExplorer FileExplorer
        {
            get
            {
                if (fileExplorer == null)
                {
                    DependencyObject obj = this;
                    while (obj != null && !(obj is UFileExplorer))
                    {
                        obj = VisualTreeHelper.GetParent(obj);
                    }
                    fileExplorer =  obj as UFileExplorer;
                }
                return fileExplorer;
            }
        }

        public static FilePathType CheckPathType(string path)
        {
            if (System.IO.Directory.Exists(path))
            {
                return FilePathType.Directory;
            }
            if (System.IO.File.Exists(path))
            {
                int starPos = path.LastIndexOf(".");
                if (starPos >= 0 && starPos < path.Length)
                {
                    String suffix = path.Substring(starPos + 1).ToLower();

                    if (suffix == "jpg" || suffix == "png" || suffix == "bmp")
                    {
                        return FilePathType.Photo;
                    }
                    else if (suffix == "avi" || suffix == "rmvb" || suffix == "wmv")
                    {
                        return FilePathType.Video;
                    }
                    else if (suffix == "doc" || suffix == "docx" || suffix == "txt")
                    {
                        return FilePathType.Document;
                    }
                    else if (suffix == "mp3" || suffix == "wma")
                    {
                        return FilePathType.Music;
                    }
                }
            }

            return FilePathType.Other;
        }
    }

    public class FileItemData: DependencyObject
    {
        public FileItemData(String path)
        {
            this.FilePath = path;
        }

        public String FilePath
        {
            get { return (String)GetValue(FilePathProperty); }
            set { SetValue(FilePathProperty, value); }
        }

        public static readonly DependencyProperty FilePathProperty =
            DependencyProperty.Register("FilePath", typeof(String), typeof(FileItemData), new UIPropertyMetadata(String.Empty));


        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected", typeof(bool), typeof(FileItemData), new UIPropertyMetadata(false));
    }


    public enum FilePathType
    {
        Directory,
        Photo,
        Music,
        Video,
        Document,
        Other
    }
}
